package com.app.micaihu.view.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.BaseOldActivity;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.custom.components.ParamMap;
import com.app.micaihu.manager.UserInforManager;
import com.app.micaihu.utils.DialogUtils;
import com.app.micaihu.utils.HttpUtils;
import com.app.micaihu.listener.HttpListener;
import com.app.utils.util.ToastUtils;
import com.google.gson.reflect.TypeToken;

/**
 * Created by Guo on 2016/7/24.
 * 更改密码
 */
public class ChangePassActivity extends BaseOldActivity implements View.OnClickListener {
    private EditText etPhoneNum;
    private ImageView ivInputCancel;
    private TextView tvNext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addContentView(R.layout.activity_findpassword);
        initView();

        initListener();
    }

    private void initView() {
        setTitle("更改密码");

        etPhoneNum = (EditText) findViewById(R.id.et_phonenum);

        ivInputCancel = (ImageView) findViewById(R.id.iv_input_cancel);

        tvNext = (TextView) findViewById(R.id.tv_next);
        tvNext.setEnabled(false);
    }

    private void initListener() {
        ivInputCancel.setOnClickListener(this);
        tvNext.setOnClickListener(this);

        etPhoneNum.addTextChangedListener(tw);
    }

    /**
     * edittext的监听
     */
    TextWatcher tw = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (etPhoneNum == null || tvNext == null) return;
            if (etPhoneNum.getText().toString().trim().length() >= 8) {
                tvNext.setEnabled(true);
            } else {
                tvNext.setEnabled(false);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_input_cancel:
                clearPhoneNum();
                break;
            case R.id.tv_next:
                next();
                break;
        }
    }


    /**
     * 清空用户名
     */
    private void clearPhoneNum() {
        if (etPhoneNum != null) {
            etPhoneNum.setText("");
        }
    }


    /**
     * 验验证手机号
     */
    private void next() {
        if (etPhoneNum == null) {
            ToastUtils.showText("程序异常,请重新打开页面");
            return;
        }
        final String phoneNumber = etPhoneNum.getText().toString().trim();
        if (TextUtils.isEmpty(phoneNumber)) {
            ToastUtils.makeText("请填写手机号");
            return;
        }
        if (UserInforManager.getInstance().getUserInfor() != null && !TextUtils.equals(phoneNumber, UserInforManager.getInstance().getUserInfor().getMobile())) {
            ToastUtils.makeText("请填写绑定的手机号");
            return;
        }

        //访问网络
        //Map<String,String> params = new HashMap<>();
        if (request_state) return;
        ParamMap params = new ParamMap();

        params.putEncode("mobile", phoneNumber);
        params.putEncode("type", TypeConstant.CHANGE_PWD2);
        params.putEncode("uid", UserInforManager.getInstance().getUserInfor().getUid());
        params.putEncode("newMobile", "");
        HttpUtils.ParmsMapMD5(params);

        addPostRequest(URIContant.CHECK_MOBILE, new TypeToken<DataBean>() {
        }.getType(), params, new HttpListener<DataBean>() {
            @Override
            public void onStart() {
                super.onStart();
                request_state = true;
                DialogUtils.getInstance().showProgressDialog(ChangePassActivity.this, "正在跳转");

            }

            @Override
            public void onError(VolleyError error) {
                request_state = false;
                ToastUtils.makeText(AppApplication.getInstance().getResources().getString(R.string.neterror));
                DialogUtils.getInstance().dismissProgressDialog();
            }

            @Override
            public void onSuccess(DataBean response) {
                if (!response.noError()) {//失败了
                    ToastUtils.makeText(response.getMsg());
                    DialogUtils.getInstance().dismissProgressDialog();
                } else {
                    DialogUtils.getInstance().dismissProgressDialog();
                    //跳转至下一个界面
                    Intent intent = new Intent(ChangePassActivity.this, ResetPwdActivity.class);
                    intent.putExtra("phone", phoneNumber);
                    intent.putExtra(TypeConstant.ACTION_TYPE, TypeConstant.RESETPWD_ACTION);
                    startActivityForResult(intent, Config.ResultCode.REQUESTCODE_1);
                }

                request_state = false;
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent arg2) {
        super.onActivityResult(requestCode, resultCode, arg2);
        if (requestCode == Config.ResultCode.REQUESTCODE_1 && resultCode == RESULT_OK) {
            setResult(resultCode);
            onBackPressed();
        }
    }

}
